/*
 * @Author: Paco
 * @Date:   2017-02-10
 * +----------------------------------------------------------------------
 * | jqadmin [ jq酷打造的一款懒人后台模板 ]
 * | Copyright (c) 2017 http://jqadmin.jqcool.net All rights reserved.
 * | Licensed ( http://jqadmin.jqcool.net/licenses/ )
 * | Author: Paco <admin@jqcool.net>
 * +----------------------------------------------------------------------
 */

layui.define(['jquery', 'laytpl', 'layer', 'modal', 'ajax', 'laypage', 'form'], function(exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        tpl = layui.laytpl,
        modal = layui.modal,
        ajax = layui.ajax,
        form = layui.form(),
        laypage = layui.laypage,
        dtable = function() {
            this.options = {
                method: "GET",
                dataType: "json",
                timeout: 50000,
                cache: false,
                url: '',
                data: '',
                tplid: "list-tpl",
                listid: "list",
                pageid: "", //如果留空则不分页
                curr: 1,
                pages: 0 //分页的总页数，通过服务端获取
            };
        };



    /**
     *@设置请求参数
     *@param string id 渲染模板的ID
     *备注：模板需配置data-params属性，值需严格的json格式，如：'{"content": "add-article.html", "title": "{{item.title}}"}'
     */
    dtable.prototype.setOption = function(id) {

    }

    /**
     *@todo 设置url中page参数为当前点击的页数
     *@param int curr 需要跳转到的页数（即点击分页按钮后的页数）
     */
    dtable.prototype.setUrlPage = function(curr) {

    }

    /**
     *@todo 把表单参数拼成url
     */
    dtable.prototype.setFormUrl = function() {
        var _this = this;
        form.on('submit(search)', function(data) {
            var params = $.param(data.field);
            if (params == "" || params == undefined) return false;
            var url = $(data.form).attr('action');
            if (url.indexOf("?") != -1) {
                _this.options.url = url + "&" + params;
            } else {
                _this.options.url = url + "?" + params;
            }
            _this.ajax();
            return false;
        })
    }

    /**
     *@todo 向服务请求数据
     */
    dtable.prototype.ajax = function(curr) {

    }

    /**
     *@todo 数据绑定到模板视图
     *@param json data 后台取得的数据
     */
    dtable.prototype.render = function(data) {
        var _this = this;
        var getTpl = $('#' + _this.options.tplid).html();

        tpl(getTpl).render(data, function(html) {
            $('#' + _this.options.listid).html(html);
        })
        _this.selectAll();
        modal.init();
        ajax.init();
        form.render();
    }


    dtable.prototype.selectAll = function() {

    }

    dtable.prototype.page = function() {
        var _this = this;
        laypage({
            cont: $(_this.options.pageid),
            pages: _this.options.pages,
            first: 1,
            last: _this.options.pages,
            prev: '<em><</em>',
            next: '<em>></em>',
            jump: function(obj, first) {
                if (!first) {
                    _this.setUrlPage(obj.curr);
                }
            }
        });
    }

    exports('dtable', dtable);
});